<?php defined('SYSPATH') or die('No direct script access.');

class Privacy  {
    
    private $user_asking_is_login = false; 
    private $the_same_person = false;
    private $have_mutual_friend = false;
    private $is_in_contact = false;
    private $user_asked_privacy = null; //model with privacy asked_user data 
    
    private function __construct(){}

    static public function factory($user_asking_id, $user_asked_id)
    {
        $privacy  = new Privacy();  
        
        //check if user asking is login
        $privacy->user_asking_is_login = (bool) $user_asking_id;
        
        //check if user_asking is friend with $user_asked 
        $contact_model = ORM::factory('contact');  
        $privacy->is_in_contact = $contact_model->in_contact($user_asking_id, $user_asked_id);

        //check if user asking and user asked is the same person
        $privacy->the_same_person = ($privacy->user_asking_is_login AND $user_asking_id == $user_asked_id);
        
        //check if they have mutual friends 
        if ($privacy->user_asking_is_login AND !$privacy->the_same_person)
        {
            $privacy->have_mutual_friend = (bool) count($contact_model->get_mutual_friends($user_asking_id, $user_asked_id));
        }
        

        $privacy->user_asked_privacy = ORM::factory('user_privacy')->where('user_id', '=',  $user_asked_id)->find();
        
        return $privacy; 
        
    }
    
    public function view_profile_permission()
    {
        if($this->the_same_person) return true; 
        
        $view_profile_permission = true;
        switch ($this->user_asked_privacy->view_profile_permission)
        {
                case 1:
                    $view_profile_permission = true;
                    break;
                case 2: //user have to be logged in
                    if(!$this->user_asking_is_login)
                        $view_profile_permission = false;
                    break; 
                case 3: //user have to be mutual friend or be direct friend or be the same person
                    if(!$this->have_mutual_friend AND !$this->is_in_contact)
                        $view_profile_permission = false;
                    break;
                case 4: //have to be in direct contact with user or be the same person
                    if(!$this->is_in_contact)
                        $view_profile_permission = false;
                    break;
                    
                default: return false;
        }
        
        return $view_profile_permission; 
        
    }
    
    public function send_message_permission()
    {
            
            $send_message_permission = !($this->the_same_person); //cant be the same person
            switch ($this->user_asked_privacy->send_message_permission)
            {
                case 1: //user have to be logged in
                    if(!$this->user_asking_is_login)
                        $send_message_permission = false;
                    break; 
                case 2: //user have to be mutual friends or be direct friend
                    if(!$this->have_mutual_friend AND !$this->is_in_contact)
                         $send_message_permission = false;
                    break;
                case 3: //have to be in direct contact with user 
                    if(!$this->is_in_contact)
                         $send_message_permission = false;
                    break;
                    
                default: return false;
            }
            
            return  $send_message_permission;
    }
    
    public function send_invitation_permission()
    {
        $send_invitation_permission = !($this->the_same_person); //cant be the same person
        
        if($this->is_in_contact) // if already in contact
            $send_invitation_permission = false;
        
        switch ($this->user_asked_privacy->send_invitation_permission)
        {
                case 1: //user have to be logged in
                    if(!$this->user_asking_is_login)
                        $send_invitation_permission = false;
                    break; 
                case 2: //user have to have mutual friends or be friend and have to be logged in 
                    if(!$this->have_mutual_friend AND !$this->is_in_contact)
                         $send_invitation_permission = false;
                    break;
                case 3: //nobody can send me invitation to contact                   
                         $send_invitation_permission = false;
                    break;
                
                default: return false;
        }
        
        return $send_invitation_permission;
    }
    
    public function comment_project_permission()
    {
        $comment_project_permission = true; 
        switch ($this->user_asked_privacy->comment_project_permission)
        {
                case 1: //user have to be logged in
                    if(!$this->user_asking_is_login)
                        $comment_project_permission = false;
                    break; 
                case 2: //user have to have mutual friends or be friend and have to be logged in 
                    if(!$this->have_mutual_friend AND !$this->is_in_contact)
                         $comment_project_permission = false;
                    break;
                case 3: //have to be in direct contact with user 
                    if(!$this->is_in_contact)
                         $comment_project_permission = false;
                    break;
                    
                case 4: //nobody can comment my project                   
                    $comment_project_permission = false;
                    break;
                
                default: return false;
        }
        
        return $comment_project_permission;
        
    }
}